//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    booolean:true,
    display:"",
    userid: "",
    uploadFileTip: '上传图片',
    imgPath: "",
    IDcard: "",
    userName: "",
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },

  //进入页面获取用户CODE
  onLoad: function () {
    app.globalData.redirectTo = app.globalData.redirectTo + 1;
    var that = this;
    wx.login({
      success: function (res) {
        var openid = res.code;
        console.log(res.code)

        wx.request({
          url: app.data.ull +'wxfettlerLoad',//接口地址
          data: { code: res.code },
          header: {
            'content-type': 'application/json' //默认值
          },
          success: function (res) {
            console.log(res)
            that.setData({
             userid: res.data,
            })
            app.globalData.userid = res.data.userid;
            app.globalData.fettlerid = res.data.fettlerid;
            app.globalData.authstate = res.data.authstate;
            console.log(app.globalData.userid)
            console.log(app.globalData.fettlerid)
            console.log(app.globalData.authstate)

            if (app.globalData.fettlerid == "null"){
              wx.showToast({
                title: '未注册',
              },3000)
            }
            if (app.globalData.authstate == 0) {
              wx.showModal({
                title: '提示',
                content: '正在认证中......',
                showCancel: false,
                confirmText: "确定",
                success: function(res) {
                  wx.switchTab({
                    url: '../index/index',
                  })
                }
              })
            }

            if (app.globalData.authstate == 1) {
              wx.showModal({
                title: '提示',
                content: '您已注册成功，正在跳转页面',
                showCancel: false,
                confirmText: "确定",
                success: function() {
                  wx.navigateTo({
                    url: '../allorder/allorder',
                  })
                }
              })
            }
          }
        })
      }
    })
  },
  onShow:function(){
   
    // if (app.globalData.authstate == 1 && app.globalData.redirectTo >0) {
    //   this.setData({
    //     booolean:false
    //   })
    //     wx.navigateTo({
    //       url: '../allorder/allorder',
    //     })
    //   }
    this.onLoad();
  },
  //获取用户输入的姓名
  userNameInput: function (e) {
    this.setData({
      userName: e.detail.value,
    })
  },
  //获取用户输入的身份证号
  IDcardInput: function (e) {
    this.setData({
      IDcard: e.detail.value,
    })
  },
  chooseImg: function () {
    var that = this
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        that.setData({
          imgPath: res.tempFilePaths[0],
          display : "none"
        })
        // console.log(res.tempFilePaths)
      }
    })
  },


  uploadImg: function (e) {
    //验证图片上传
    if (this.data.imgPath == "") {
      wx.showModal({
        title: '提示',
        content: '图片未选择，请选择图片上传',
      })
    }

    //验证身份证号码输入
    var IDCard = this.data.IDcard;
    if (IDCard == "") {
      wx.showModal({
        title: '提示',
        content: '身份证号码输入为空，请输入身份证号码',
      })
    }
    //验证姓名输入
    var userName = this.data.userName;
    if (userName == "") {
      wx.showModal({
        title: '提示',
        content: '姓名输入为空，请输入姓名',
      })
    }
  
    if(userName != "" && IDCard != "" && this.data.imgPath != ""){
      var that = this;
      wx.uploadFile({
        url: app.data.ull + 'wxfettlerController',
        header: {
          'content-type': 'application/json' //默认值
        },
        filePath: this.data.imgPath,
        name: 'filePath',
        formData: {
          IDcard: this.data.IDcard,
          userName: this.data.userName,
          userid: app.globalData.userid
        },
        success: function (res) {
          console.log(res)
          if (res.data == "姓名错误") {
            wx.showModal({
              title: '提示',
              content: '姓名输入错误，请重新输入',
            })
          } else if (res.data == "身份证错误") {
            wx.showModal({
              title: '提示',
              content: '身份证输入格式错误，请重新输入',
            })
          } else if (res.data == "false") {
            wx.showModal({
              title: '提示',
              content: '本账号已注册，无法再次注册',
            })
          } else {
            wx.showToast({
              title: '注册成功',
            })
            that.setData({
              imgPath: "",
              idcard: "",
              username: ""
            })
          }
        },
        fail: function (data) {
          console.log(data)    
        }
      })
    }
  }
})
